<?php
require 'TVB/pms/pdes/common/bootstrap.php';

empty($_REQUEST['type']) && exit('error');


switch($_REQUEST['type']){
    case 'liberal_tag':
		getTags(4);
        break;
    case 'source':
		getTags(5);
        break;
	case 'tag': 
		getTags(1);
		break;
	case 'chapter_nature': 
		getTags(3);
		break;
	case 'programme_nature': 
		getTags(2);
		break;
	case 'cpl_cat':
	case 'category': 
        if (empty($_REQUEST['format'])  ||  'html'  !=  $_REQUEST['format'])    getCategory();
        else    getCategoryHtml();
		break;
	case  'subCategory':
		getSubCategory();
		break;
	case 'getTime': 
		echo date('Y-m-d H:i');
		break;
	case 'tvb.com_cat':
	case 'tvbcategory':
		getTvbCategory();
		break;
	case 'tvbcom_auto':
		getAutoTvbCategory();
		break;
}

/**
 * auto complete TVBcom Category
 */
function getAutoTvbCategory()
{
	$pid = $_REQUEST['pid'];
	$sid = $_REQUEST['sid'];
	$tvbcom_category = new programme_tvbcom_category();
	if ($sid == 0)
	{
		$tvbcom_category_list = $tvbcom_category->getTvbcomCategory($pid);
	}
	else
	{
		$tvbcom_category_list = $tvbcom_category->getTvbcomCategory($pid, $sid);
	}
	$json = array();
	if (!empty($tvbcom_category_list))
	{
		$i = 0;
		foreach ($tvbcom_category_list as $category)
		{
			$parent_name = tvbcom_category::getParentName($category->title, $category->parent_id);
			$json[$i] = array('id'=>$category->tvbcom_category_id, 'title'=>$parent_name." ".$category->title);
			$i++;
		}
	}
	echo json_encode($json);
}
/**
 * Api for programme find selects!
 */
function getSubCategory()
{
	$id		=	$_REQUEST['id'];
	$cate	=	new	cpl_category();
	$json	=	$cate->getSubCategorys($id);
    empty($json)    &&  $json   =   array();
	echo json_encode($json);
}

/**
 * API get category to html
 */
function getCategoryHtml()
{
    $cate   =   new cpl_category();
    $root   =   $cate->getSubCategorys(0);

    $html   =   NULL;
    if (!empty($root)   &&  is_array($root))
    {
        $html   =   '
                <ul>';
        foreach ($root  as  $key => $value)
        {
            if (empty($key))    continue;
            $html   .=   '
                    <li><h4>'. $value .'</h4>';
            $subCate=   $cate->getSubCategorys($key);
            if (!empty($subCate) &&  is_array($subCate))
            {
                $html   .=  '
                        <ul class="sub">';
                foreach ($subCate   as  $subID  =>  $subValue)
                {
                    $html   .=  '
                            <li><a href="#'. $subID. '">'. $subValue. '<a/></li>';
                }
                $html   .=  '
                        </ul>';
            }
            $html   .=  '
                    </li>';
        }
        $html   .=  '
                </ul>';
    }
    echo    $html;
}

function getTags($type){/*{{{*/
	global $config;

	/*
	$tag_type = new tag_type();
	$ret_type = $tag_type->findByInSql( (string)$config->allow_tag_type );
	 */

	$tag = new tag();
	$tag_data = $tag->getByType($type);

	$json_data = array();
	$i = 0;
	foreach ($tag_data as $tag)
	{
		$json_data[$i] = array("id"=>$tag->id, "name"=>$tag->system_label);
		$tag->getSubTag($tag->id, $json_data[$i]);
		$i++;
	}

    empty($json_data)   &&  $json_data  =   array();
	echo json_encode($json_data);
}/*}}}*/

function getCategory()/*{{{*/
{
	$category = new cpl_category();
	$p_data = $category->getParent();

	$json_data = array();
	$i = 0;
	foreach ($p_data as $category)
	{
		$json_data[$i] = array("id"=>$category->id,"name"=>$category->system_label,"child"=>array());
		$category->getSub($category->id,$json_data[$i]);
		$i++;
	}

	$id		=	0;
	isset($_REQUEST['id'])	&&	$id	=	trim($_REQUEST['id']);

	if (empty($id))
	{
		echo json_encode($json_data);
		exit;
	}
    $json_data  =   $category->getSubCategorys($id);
    empty($json_data)   &&  $json_data  =   array();

	echo json_encode($json_data);
}/*}}}*/

function getTvbCategory()/*{{{*/
{
	$category = new tvbcom_category();
	$p_data = $category->getParent();

	$json_data = array();
	$i = 0;
	foreach ($p_data as $category)
	{
		$json_data[$i] = array("id"=>$category->id,"name"=>$category->system_label,"child"=>array());
		$category->getSub($category->id,$json_data[$i]);
		$i++;
	}

	$id		=	0;
	isset($_REQUEST['id'])	&&	$id	=	trim($_REQUEST['id']);

	if (empty($id))
	{
		echo json_encode($json_data);
		exit;
	}
    $json_data  =   $category->getSubCategorys($id);
    empty($json_data)   &&  $json_data  =   array();

	echo json_encode($json_data);

}/*}}}*/
?>
